<template>
	<view>
		<form @submit="handleSubmit">
			<input placeholder="教务系统账号" name="username" class="username"></input>
			<input placeholder="教务系统密码" name="password" password class="password"></input>
			<!-- <input v-if="useVPN" name="vpnusername" placeholder="校园网账号" class="vpn-username"></input> -->
			<!-- <input v-if="useVPN" name="vpnpassword" placeholder="校园网密码" password class="vpn-password"></input> -->
			<input name="semester" placeholder="学期(非必须 格式 2019-2020-1)" />
			<button form-type="submit" type="primary">验证</button>
		</form>
		<w-loading click="false" ref="loading"></w-loading>
		<uni-popup type="top" ref="message">
			<view class="message">
				{{message}}
			</view>
		</uni-popup>
		<text class="copy-right">Copyright © 2021 mdreamfever, all rights reserved.</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				message: "发生了错误",
			}
		},
		methods: {
			handleSubmit(e) {
				this.$refs.loading.open()
				let value = e.detail.value
				console.log(value)
				let username = value.username
				let passowrd = value.password
				let semester = ""
				let date = new Date()
				let year = date.getFullYear()
				let month = date.getMonth() + 1
				if (month >= 8) {
					semester = `${year}-${year+1}-1`
				} else {
					semester = `${year-1}-${year}-2`
				}
				console.log(`开课学期:${semester}`)
				let loginData = {
					"username": username,
					"password": passowrd,
					"semester": semester,
				}
				if (value.semester) {
					loginData.semester = value.semester
				}
				let url = `${getApp().globalData.appConfig.host}/cloud/just/course`
				try {
					uni.request({
						url: url,
						data: loginData,
						method: "GET",
						success: (res) => {
							console.log(res)
							this.$refs.loading.close()
							let response = res.data
							let httpStatusCode = response.httpStatusCode
							if (httpStatusCode.value !== 200) {
								console.log(response)
								this.message = response.data
								this.$refs.message.open()
							} else {
								let setting = getApp().globalData.settingData
								setting.username = username
								setting.password = passowrd
								setting.currentSemester = loginData.semester
								setting.scoreQuerySemester = loginData.semester
								uni.setStorage({
									key: "setting_data_key",
									data: setting
								})
								uni.setStorage({
									key: "table_data_key",
									data: response.data,
								})
								getApp().globalData.tableData = response.data
								getApp().globalData.settingData = setting
								console.log("login success")
								uni.switchTab({
									url: "../index/index"
								})
							}
						},
						fail: (res) => {
							console.log(res)
							this.$refs.loading.close()
							this.message = "连接服务器失败"
							this.$refs.message.open()
						}
					})
				} catch (e) {
					console.log(e)
					this.$refs.loading.close()
					this.message = "发生了错误"
					this.$refs.message.open()
				}
			}
		}
	}
</script>

<style>
	input {
		margin: 20rpx;
		padding: 20rpx;
		background-color: #f8f8f8;
		border-radius: 20rpx;
		font-size: .8em;
	}

	button {
		margin: 20rpx;
	}
</style>
